﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LinqToTwitter;

namespace Twitter
{
    public partial class Amis : System.Web.UI.Page
    {
        public String userId;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Util.Authentification authen = new Util.Authentification();
                Boolean b = authen.isAuthentificated(Request, Response);

                if (!b)
                    authen.auth.BeginAuthorization(Request.Url);
                else
                {
                    Session["auth"] = authen.auth;
                }

                userId = authen.auth.UserId;
            }
        }

        public IEnumerable<LinqToTwitter.User> getFriendList()
        {
            Util.UserTweets friendTweets = new Util.UserTweets(Request, Response, userId);

            return friendTweets.getFriend(Session["auth"] as WebAuthorizer);
        }

        public void saveAllUsersPost()
        {
            this.saveResult.Text = "";
            IEnumerable<LinqToTwitter.User> users = getFriendList();
            Util.UserTweets persist = new Util.UserTweets(Request, Response, userId);
            foreach(LinqToTwitter.User user in users)
             persist.PersistTweets(Session["auth"] as WebAuthorizer, persist.getUser(Session["auth"] as WebAuthorizer, user.Identifier.UserID), Connexion.getConnectedUserName());
            this.saveResult.Text = "Les données ont été bien enregistrées";        
        }

        protected void saveAllClicked(object sender, EventArgs e)
        {
            saveAllUsersPost();
        }

        protected void refreshTweets(object sender, EventArgs e)
        {
     
        }

    }
}